package {
	import flash.display.*;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.*;
	import flash.text.TextField;
	import flash.utils.Timer;

	public class Szene extends Sprite 
	{
		var moveContainer:MovieClip = new MovieClip();
		var moveTimer:Timer = new Timer(30);
		var objects:Array = new Array();
		var player:Player;
		var coords:TextField = new TextField();
		
		const gravity_constant:Number = 0.3;
		const xBounds:Array = [-500,500];
		const yBounds:Array = [-500,500];
		const gridSize = 20;
		const numObjects = 10;
		
		public function Szene() {
			stage.stageWidth = 400;
			stage.stageHeight = 400;
			stage.frameRate = 30;
			
			moveTimer.addEventListener(TimerEvent.TIMER, onTick);
			stage.addEventListener(MouseEvent.CLICK, onClick);
			
			// Raster zeichnen
			var lines:Sprite = new Sprite();
			lines.graphics.lineStyle(1,0x636363,0.5,true,"normal","round","round",1);
			
			for(var i:int = xBounds[0]; i <= xBounds[1]; i += gridSize) {
				if(i == 0) lines.graphics.lineStyle(1,0xFF0000,1,true,"normal","round","round",1);
				else lines.graphics.lineStyle(1,0x636363,0.5,true,"normal","round","round",1);
				lines.graphics.moveTo(i,xBounds[0]);
				lines.graphics.lineTo(i,xBounds[1]);
			}
			
			for(var j:int = yBounds[0]; j <= yBounds[1]; j += gridSize) {
				if(j == 0) lines.graphics.lineStyle(1,0xFF0000,1,true,"normal","round","round",1);
				else lines.graphics.lineStyle(1,0x636363,0.5,true,"normal","round","round",1);
				lines.graphics.moveTo(yBounds[0],j);
				lines.graphics.lineTo(yBounds[1],j);
			}
			
			moveContainer.addChild(lines);
			
			stage.addChild(moveContainer);
			
			initGame();
		}
		
		function initGame():void {
			
			coords.text = "";
			coords.width = 400;
			coords.x = 10;
			coords.y = 10;
				
			player = new Player(stage,moveContainer,xBounds,yBounds);
			player.setValues(new Point(200,200),new Point(0,0),50,3,0.001);
			objects.push(player);
			
			coords.text += "player: ("+player.relPos.x+";"+player.relPos.y+")\n"; 
			
			for(var c:int = 0; c < numObjects; c++) {
				var curObj:Objekt = new Objekt(moveContainer,xBounds,yBounds);
				var xPos:int = xBounds[0]+Math.random()*(xBounds[1]-xBounds[0]);
				var yPos:int = yBounds[0]+Math.random()*(yBounds[1]-yBounds[0]);
				curObj.setValues(new Point(xPos,yPos),new Point(0,0), 10+Math.random()*80,3,0.001);
				objects.push(curObj);
				coords.text += "obj"+c+": ("+curObj.x+";"+curObj.y+")\n";
			}
			
			stage.addChild(coords);
			moveTimer.start();
		}
		
		function onTick(evt:TimerEvent):void 
		{
			coords.text = "";
			for each (var cur_obj:Objekt in objects ) 
			{
				for each (var ref_obj:Objekt in objects )
				{
					if ( cur_obj != ref_obj)
					{
						var cr_vec:Point = new Point(ref_obj.x-cur_obj.x,ref_obj.y-cur_obj.y);
						if(player == cur_obj) cr_vec = new Point(ref_obj.x-player.relPos.x,ref_obj.y-player.relPos.y);
						var dist:Number = Math.sqrt(cr_vec.x*cr_vec.x + cr_vec.y*cr_vec.y);
												
						if ( dist > 5.5 )
						{
							var gravity_vec:Point = new Point(0,0);
							
							gravity_vec.x = gravity_constant * (ref_obj.mass * cur_obj.mass)/(dist*dist) * (cr_vec.x/dist);
							gravity_vec.y = gravity_constant * (ref_obj.mass * cur_obj.mass)/(dist*dist) * (cr_vec.y/dist);
							
							var vl:Number = Math.sqrt(gravity_vec.x*gravity_vec.x + gravity_vec.y*gravity_vec.y);
							
							cur_obj.direction.x += gravity_vec.x / vl;
							cur_obj.direction.y += gravity_vec.y / vl;
							
							//cur_obj.direction.x += gravity_vec.x;
							//cur_obj.direction.y += gravity_vec.y;
						}
						if(dist < (cur_obj.radius + ref_obj.radius) && cur_obj.mass >= ref_obj.mass) {
							cur_obj.grow(ref_obj.mass);
							ref_obj.graphics.clear();
							objects.splice(objects.indexOf(ref_obj),1);
							stage.removeChild(ref_obj);
							
							var newObj:Objekt = new Objekt(moveContainer,xBounds,yBounds);
							var xPos:int = xBounds[0]+Math.random()*(xBounds[1]-xBounds[0]);
							var yPos:int = yBounds[0]+Math.random()*(yBounds[1]-yBounds[0]);
							newObj.setValues(new Point(xPos,yPos),new Point(0,0), 10+Math.random()*80,3,0.001);
							objects.push(newObj);
						}
					}
				}
								
				cur_obj.moveObject();
				if(cur_obj == player) coords.text += "player: ("+player.relPos.x+";"+player.relPos.y+")\n"; 
				else coords.text += "obj"+objects.indexOf(cur_obj)+": ("+cur_obj.x+";"+cur_obj.y+")\n";
			}
		}
		
		function onClick(evt:MouseEvent):void {
			player.target = new Point(stage.mouseX,stage.mouseY);
		}
	}
}